<?php

class Bid_ItemController extends Bid_Library_Controller_Action
{
    public function init()
    {
        parent::init();

        $this->_model = new Model_BidItem();
        $options = array('label_class' => 'label1');
        $this->_form = new Bid_Form_Item($options);
    }

    /**
     * @todo Create an Item Bid
     * @return void
     */
    public function createAction()
    {
        // redirect to login page if user not login yet
        if (empty($this->view->userProfile)) {
            $urlOptions = array(
                'module' => 'user',
                'controller' => 'auth',
                'action' => 'login'
            );
            $this->_helper->redirector->gotoRoute($urlOptions);
        }
        
        // Start
        $config = Zend_Registry::get('configuration');
        $request = $this->getRequest();

        // submit form
        if ($request->isPost() && $this->_form->isValid($request->getPost())) {

            // Processing for upload file
            if($this->_form->file->isUploaded()) {
                $original_file_name = $this->_form->file->getFileName('', false);
                $source = $this->_form->file->getFileName();
                $ext = pathinfo($source, PATHINFO_EXTENSION);
                $new_image_name = str_replace('.' . $ext, '', $original_file_name). '-' . time();
                $new_image_name_ext = $config->upload->path . DS . 'images' . DS . 'bid' . DS . $new_image_name . '.' . $ext;

                $this->_form->file->addFilter('Rename', array(
                    'target' => $new_image_name_ext,
                    'overwrite' => true));

                if ($this->_form->file->receive()) {
                    $this->getRequest()->setParam('image', $new_image_name . '.' . $ext);

                    // Create Thumbnails
                    include_once LIB_PATH . '/PhpThumb/ThumbLib.inc.php';
                    $thumbnails = array();
                    $thumb = PhpThumbFactory::create($new_image_name_ext);
                    $thumbList = $new_image_name . '-160x120' . '.' . $ext;
                    $thumb->adaptiveResize(160, 120)->save($config->upload->path . DS . 'images' . DS . 'bid' . DS . 'thumbnails' . DS . $thumbList);
                    $thumb = PhpThumbFactory::create($new_image_name_ext);
                    $thumbGrid = $new_image_name . '-218x164' . '.' . $ext;
                    $thumb->adaptiveResize(218, 164)->save($config->upload->path . DS . 'images' . DS . 'bid' . DS . 'thumbnails' . DS . $thumbGrid);
                    $thumb = PhpThumbFactory::create($new_image_name_ext);
                    $thumbHome = $new_image_name . '-218xn' . '.' . $ext;
                    $thumb->resize(218)->save($config->upload->path . DS . 'images' . DS . 'bid' . DS . 'thumbnails' . DS . $thumbHome);
                    $thumbnails['thumb_list'] = $thumbList;
                    $thumbnails['thumb_grid'] = $thumbGrid;
                    $thumbnails['thumb_home'] = $thumbHome;
                    $this->getRequest()->setParam('thumbnails', json_encode($thumbnails));

                    // Create Gallery
                    $galleries = array();
                    if($this->_form->files->isUploaded()) {
                        $images = $this->_form->files->getFileInfo();
                        foreach ($images as $image) {
                            $galleries[] = $image['name'];
                        }
                        $this->_form->files->receive();
                    }
                    $this->getRequest()->setParam('images', json_encode($galleries));

                    $result = $this->save();
                    $this->_helper->flashMessenger->addMessage($result);
                    if ($result['result'] == 'success') {
                        $urlOptions = array('module'=>'bid', 'controller'=>'item', 'action'=>'list', 'value'=>'successful');
                        $this->_helper->redirector->gotoRoute($urlOptions);
                    }
                }
            }
        }

        $this->view->form = $this->_form;
    }

    public function detailAction() {
        $request = $this->getRequest();
        $itemId = $request->getParam('id', 0);
        if(is_numeric($itemId) && $itemId > 0) {
            $item = $this->_model->find($itemId);
            if(is_object($item)) {
                $this->view->assign('item', $item);
            }
            else {
                $urlOptions = array('module'=>'default', 'controller'=>'index', 'action'=>'index');
                $this->_helper->redirector->gotoRoute($urlOptions);
            }
        }
        else {
            $urlOptions = array('module'=>'default', 'controller'=>'index', 'action'=>'index');
            $this->_helper->redirector->gotoRoute($urlOptions);
        }
    }

    private function save()
    {
        $msg = array();
        if (is_object($this->_model)) {
            try {
                $attributes = $this->_model->buildAttributes($this->getRequest()->getParams());
                $this->_model->setOptions($attributes);
                $result = $this->_model->save();
                if ($result) {
                    $msg = array(
                        'id' => $result,
                        'result' => 'success',
                        'message' => $this->view->translate('MSG_SUCCESS_SAVE')
                    );
                } else {
                    $msg = array(
                        'result' => 'failed',
                        'message' => $this->view->translate('MSG_FAILED_SAVE')
                    );
                }
            } catch (Zend_Exception $e) {
                $msg = array(
                    'result' => 'failed',
                    'message' => $e->getMessage()
                );
            }
        } else {
            $msg = array(
                'result' => 'error',
                'message' => $this->view->translate('MSG_ERROR_MODEL_NOT_INITIAL')
            );
        }

        return $msg;
    }
}